package com.small.act;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.small.R;

public class CityListActivity extends Activity{

	private BaseAdapter adapter;  
	private ListView personList;  

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	 
		setContentView(R.layout.city_list);
		personList = (ListView) findViewById(R.id.city_list);  
		onQueryComplete();
	}
	@Override  
	protected void onResume() {   
		super.onResume();  
	}  
	
	protected void onQueryComplete() {  
		List<Model> list=new ArrayList<CityListActivity.Model>();
		Model model= new Model();
		model.setTxt("胡为保护");
		model.setTxtname("A");
		list.add(model);
		Model model1= new Model();
		model1.setTxt("为保护");
		model1.setTxtname("A");
		list.add(model1);
		Model model2= new Model();
		model2.setTxt("保护");
		model2.setTxtname("A");
		list.add(model2);list.add(model2);list.add(model2);list.add(model2);list.add(model2);list.add(model2);
		Model model3= new Model();
		model3.setTxt("护");
		model3.setTxtname("B");
		list.add(model3);
		Model model4= new Model();
		model4.setTxt("基督教");
		model4.setTxtname("C");
		list.add(model4);
		
		Model model41= new Model();
		model41.setTxt("基督教");
		model41.setTxtname("T");
		list.add(model41);
		Model model42= new Model();
		model42.setTxt("基督教");
		model42.setTxtname("F");
		list.add(model42);
		Model model43= new Model();
		model43.setTxt("基督教");
		model43.setTxtname("L");
		list.add(model43);
		
		Model model5= new Model();
		model5.setTxt("为保护");
		model5.setTxtname("C");
		list.add(model5);
		model5.setTxt("为保护");
		model5.setTxtname("D");
		list.add(model5);
		model5.setTxt("为保护");
		model5.setTxtname("G");
		list.add(model5);
		model5.setTxt("为保护");
		model5.setTxtname("G");
		list.add(model5);
		model5.setTxt("为保护");
		model5.setTxtname("Y");
		list.add(model5);
		model5.setTxt("为保护");
		model5.setTxtname("Y");
		list.add(model5);
		model5.setTxt("为保护");
		model5.setTxtname("Y");
		list.add(model5);
		model5.setTxt("为保护");
		model5.setTxtname("Y");
		list.add(model5);
		model5.setTxt("为保护");
		model5.setTxtname("Y");
		list.add(model5);
		model5.setTxt("为保护");
		model5.setTxtname("Z");
		list.add(model5);
		model5.setTxt("为保护");
		model5.setTxtname("Z");
		list.add(model5);
		setAdapter(list);
	} 

	 private void setAdapter(List<Model> list) {  
		 adapter = new ListAdapter(this, list);  
		 personList.setAdapter(adapter);  
			
	 }  
		 	 
	 private static class ViewHolder {  
		 TextView alpha;  
		 TextView name;  
	 }  
	 class Model {
		 private String txt;
		 private String txtname;
		public String getTxt() {
			return txt;
		}
		public void setTxt(String txt) {
			this.txt = txt;
		}
		public String getTxtname() {
			return txtname;
		}
		public void setTxtname(String txtname) {
			this.txtname = txtname;
		}
		 
	 }
	 /** 
	  * 其他项目使用时，只需要传进来一个有序的list即可 
	  */  
	 private class ListAdapter extends BaseAdapter {  
		 private LayoutInflater inflater;  
		 private List<Model> list;  
		 private HashMap<String, Integer> alphaIndexer;//保存每个索引在list中的位置【#-0，A-4，B-10】  
		 private String[] sections;//每个分组的索引表【A,B,C,F...】  
		 
		 public ListAdapter(Context context, List<Model> list) {  
			 this.inflater = LayoutInflater.from(context);  
			 this.list = list; // 该list是已经排序过的集合，有些项目中的数据必须要自己进行排序。  
			 this.alphaIndexer = new HashMap<String, Integer>();  
			 
			 for (int i =0; i <list.size(); i++) {  
				 String name = list.get(i).getTxtname();
				 if(!alphaIndexer.containsKey(name)){//只记录在list中首次出现的位置  
					 alphaIndexer.put(name, i);  
				 }  
			 }  
			 Set<String> sectionLetters = alphaIndexer.keySet();  
			 ArrayList<String> sectionList = new ArrayList<String>(  
					 sectionLetters);  
			 Collections.sort(sectionList);  
			 sections = new String[sectionList.size()];  
			 sectionList.toArray(sections);  
		 }  
	 		
		 @Override  
		 public int getCount() {  
			 return list.size();  
		 }  
		 
		 @Override  
		 public Object getItem(int position) {  
			 return list.get(position);  
		 }  
	 		
		 @Override  
		 public long getItemId(int position) {  
			 return position;  
		 }  
	 		
		 @Override  
		 public View getView(int position, View convertView, ViewGroup parent) {  
			 ViewHolder holder;  
			 
			 if (convertView == null) {  
				 convertView = inflater.inflate(R.layout.city_list_item, null);  
				 holder = new ViewHolder();  
				 holder.alpha = (TextView) convertView.findViewById(R.id.alpha);  
				 holder.name = (TextView) convertView.findViewById(R.id.name);  
				 convertView.setTag(holder);  
			 } else {  
				 holder = (ViewHolder) convertView.getTag();  
			 }  
			 Model cv = list.get(position);  
			 String name = cv.getTxt();  
			 holder.name.setText(name);  
			 
			 // 首字母 
			 String currentStr = list.get(position).getTxtname();
			 // 上一个的首字母
			 String previewStr = (position - 1) >= 0 ? list.get(position-1).getTxtname() : " ";  
			 /** 
			  * 判断显示A-Z的TextView隐藏与可见 
			  */  
			 if (!previewStr.equals(currentStr)) { // 当前联首字母！=上一个首字母，说明当前当前是新组。  
				 holder.alpha.setVisibility(View.VISIBLE);  
				 holder.alpha.setText(currentStr);  
			 } else {  
				 holder.alpha.setVisibility(View.GONE);  
			 }  
			 return convertView;  
		 }  
	 }  
}
